﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;



namespace H2.NET.Web.Pages
{
    public partial class TabPlg : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }
                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }
        public string BomActions
        {
            get
            {
                return ViewState["BomActions"] == null ? "" : (string)ViewState["BomActions"]; ;
            }
            set { ViewState["BomActions"] = value; }
        }

         public bool IsHaveAttachment
        {
            get
            {
                return ViewState["IsHaveAttachment"] == null ? false : (bool)ViewState["IsHaveAttachment"];
            }
            set
            {
                ViewState["IsHaveAttachment"] = value;
            }
        }

        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            
            #region PLALOG
            if (Status.Category == "PLALOG")
            {
                bool flg1 = Status.Logistics == "C" && TaskAPI.IsAllowSubmit;
                bool flg2 = Status.Planning == "C" && TaskAPI.IsAllowSubmit;

                gv_Plg.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 20, Align.Left, (flg1 || flg2), "No", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("01", "SAP Code", BoundColumnType2.TextBox, 70, Align.Left, (flg1 || flg2), "SAP_Code", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("02", "Plant", BoundColumnType2.TextBox, 70, Align.Left, (flg1 || flg2), "Plant", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("03", "Extend Sales:general/plant(Y/N)", BoundColumnType2.DropDownList, 150, Align.Left, (flg1 || flg2), "Extend_Sales", InputType.None, new string[3] { "", "Y", "N" }));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("04", "Storage Location", BoundColumnType2.TextBox, 70, Align.Left, (flg1 || flg2), "Storage_Location", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("05", "Loading Group", BoundColumnType2.TextBox, 80, Align.Left, (flg1 || flg2), "Loading_Group", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("06", "With qty Struture(Y/N)", BoundColumnType2.DropDownList, 100, Align.Left, (flg1 || flg2), "With_qty_Struture", InputType.None, new string[3] { "", "Y", "N" }));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("07", "ICD(Y/N)", BoundColumnType2.DropDownList, 80, Align.Left, (flg1 || flg2), "ICD", InputType.None, new string[3] { "", "Y", "N" }));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("08", "Do not cost(打勾/不打勾)", BoundColumnType2.DropDownList, 70, Align.Left, (flg1 || flg2), "Do_not_cost", InputType.None, new string[3] { "", "打勾", "不打勾" }));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("09", "Plant-sp.matl status", BoundColumnType2.TextBox, 100, Align.Left, (flg1 || flg2), "Plant_spmatl_status", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("10", "SPT", BoundColumnType2.TextBox, 70, Align.Left, (flg1 || flg2), "SPT", InputType.None, null));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("11", "Deletion flag(Y/N)", BoundColumnType2.DropDownList, 150, Align.Left, (flg1 || flg2), "Deletion_flag", InputType.None, new string[3] { "", "Y", "N" }));
                gv_Plg.DynamicColumn.Add(new BoundColumn2("12", "Remark", BoundColumnType2.TextBox, 150, Align.Left, (flg1 || flg2), "Remark", InputType.None, null));
                gv_Plg.Width = 1000;
                SetEnable(flg1 || flg2, gv_Plg_Add);
                SetEnable(flg1 || flg2, gv_Plg_Delete);
                //SetVisible(gv_Plg.Enabled && gv_Plg.Visible, gv_Plg_Add, gv_Plg_Delete);
                if (!Page.IsPostBack)
                {
                    Plg_P14.Visible = true;
                    gv_Plg.Key = Key;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Material_Plalog> plg_s = new List<mdc_Material_Plalog>();
                        for (int i = 0; i < 6; i++)
                        {
                            plg_s.Add(new mdc_Material_Plalog());
                            plg_s[i].No = i + 1;
                        }
                        gv_Plg.DataSource = plg_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Material_Plalog> plg_s = (mdc_Material_Plalog.process == TaskAPI.Process & mdc_Material_Plalog.incident == TaskAPI.Incident).List<mdc_Material_Plalog>();
                        if (plg_s.Count == 0)
                        {
                            for (int i = 0; i < 6; i++)
                            {
                                plg_s.Add(new mdc_Material_Plalog());
                                plg_s[i].No = i + 1;
                            }
                        }
                        gv_Plg.DataSource = plg_s;
                    
                    }
                    #endregion

                }
            }
            #endregion

        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Plg_Add":
                    List<mdc_Material_Plalog> plg_s = (List<mdc_Material_Plalog>)gv_Plg.DataSource;
                    mdc_Material_Plalog plg = new mdc_Material_Plalog();
                    plg.No = plg_s.Count + 1;
                    plg_s.Insert(gv_Plg.ChangedRowIndex == -1 || gv_Plg.ChangedRowIndex >= plg_s.Count ? plg_s.Count : gv_Plg.ChangedRowIndex, plg);
                    gv_Plg.DataSource = plg_s;
                    gv_Plg.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Plg_Delete":
                    List<mdc_Material_Plalog> plg_s = (List<mdc_Material_Plalog>)gv_Plg.DataSource;
                    IList<mdc_Material_Plalog> list = (mdc_Material_Plalog.process == TaskAPI.Process & mdc_Material_Plalog.incident == TaskAPI.Incident).List<mdc_Material_Plalog>();
                    if (plg_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    plg_s.RemoveAt(gv_Plg.ChangedRowIndex == -1 || gv_Plg.ChangedRowIndex >=plg_s.Count ? plg_s.Count -1 : gv_Plg.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Material_Plalog plg_1 in list)
                    {
                        if (string.IsNullOrEmpty(plg_1.SAP_Code))
                        {
                            continue;
                        }
                        foreach (mdc_Material_Plalog plg_2 in plg_s)
                        {

                            if (string.IsNullOrEmpty(plg_1.SAP_Code))
                            {
                                continue;
                            }
                            if (plg_2.ID == plg_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(plg_1);
                        }
                        flag = false;
                    }

                    gv_Plg.DataSource = plg_s;
                    gv_Plg.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region PLALOG
                case "PLALOG":
                    foreach (mdc_Material_Plalog plg in (List<mdc_Material_Plalog>)gv_Plg.DataSource)
                    {
                        //if (string.IsNullOrEmpty(pkg_fini.FINI_Code))
                        //{
                        //    continue;
                        //}
                        plg.Process = TaskAPI.Process;
                        plg.Incident = TaskAPI.Incident;
                        plg.UserName = UserAPI.UserName;
                        plg.Time = System.DateTime.Now;
                        plg.Status = 0;
                        Storage.Save(plg);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            foreach (mdc_Material_Plalog plg in (List<mdc_Material_Plalog>)gv_Plg.DataSource)
            {
                if (!string.IsNullOrEmpty(plg.SAP_Code))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, plg.Plant, "&nbsp;&nbsp;Plant为必填"));
                    
                }
            }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion

    }
}